package com.newlink.building.library_rino.utils

import android.content.Context
import android.media.AudioManager
import android.util.Log


/**
 * 犀云提供的声音通道切换方法
 */
object RinoAudioUtils {
    /**
     * 切到扬声器
     */
    fun changeToSpeaker(context: Context) {
        Log.e("[RinoAudioUtils]", "changeToSpeaker")
        try {
            val audioManager = context
                .getSystemService(Context.AUDIO_SERVICE) as AudioManager
            audioManager.mode = AudioManager.MODE_NORMAL
            audioManager.isSpeakerphoneOn = true // 打开扬声器
        } catch (e: java.lang.Exception) {
            throw java.lang.RuntimeException(e)
        }

    }

    /**
     * 切到听筒
     */
    fun changeToEarpiece(context: Context) {
        Log.e("[RinoAudioUtils]", "changeToEarpiece")
        try {
            val audioManager = context
                .getSystemService(Context.AUDIO_SERVICE) as AudioManager
            audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
            audioManager.isSpeakerphoneOn = false // 关闭扬声器
            audioManager.isBluetoothScoOn = false
            audioManager.startBluetoothSco()
            audioManager.stopBluetoothSco()
            audioManager.isSpeakerphoneOn = false
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    /**
     * 切换到蓝牙
     */
    fun changeToBluetooth(context: Context) {
        Log.e("[RinoAudioUtils]", "changeToBluetooth")
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
        audioManager.startBluetoothSco()
        audioManager.isBluetoothScoOn = true
        audioManager.isSpeakerphoneOn = false
    }

    /**
     * 切换到耳机,理论就是changeToEarpiece
     */
    fun changeToHeadset(context: Context) {
        Log.e("[RinoAudioUtils]", "changeToHeadset")
        changeToEarpiece(context)
    }

    /**
     * 根据外设情况自动切换声道
     */
    fun choiceAudioModel(context: Context, isSpeakerOn: Boolean) {
        Log.e("[RinoAudioUtils]", "choiceAudioModel isSpeakerOn:$isSpeakerOn")
        if (isWiredHeadsetOn(context)) {
            changeToHeadset(context)
        } else if (isBluetoothA2dpOn(context)) {
            changeToBluetooth(context)
        } else {
            if (isSpeakerOn) {
                changeToSpeaker(context)
            } else {
                changeToEarpiece(context)
            }
        }
    }

    fun isSpeakerphoneOn(context: Context): Boolean {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.isSpeakerphoneOn
    }

    fun isWiredHeadsetOn(context: Context): Boolean {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.isWiredHeadsetOn
    }

    fun isBluetoothA2dpOn(context: Context): Boolean {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.isBluetoothA2dpOn
    }

    fun isBluetoothScoOn(context: Context): Boolean {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.isBluetoothScoOn
    }

    fun setMicrophoneMute(context: Context, mute: Boolean): Int {
        Log.e("[RinoAudioUtils]", "setMicrophoneMute mute:$mute")
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        audioManager.isMicrophoneMute = mute
        return 1
    }

    fun isMicrophoneMute(context: Context): Boolean {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        Log.e(
            "[RinoAudioUtils]",
            "isMicrophoneMute isMicrophoneMute:${audioManager.isMicrophoneMute}"
        )
        return audioManager.isMicrophoneMute
    }

    fun setAudioManagerInCommunicationMode(context: Context) {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        Log.e("[RinoAudioUtils]", "Setting audio manager in communication mode")
        audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
    }

    fun setAudioManagerInNormalMode(context: Context) {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        Log.e("[RinoAudioUtils]", "Setting audio manager in normal mode")
        audioManager.mode = AudioManager.MODE_NORMAL
    }
}